package com.example.recuitactivity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class RegisterActivity extends FragmentActivity {
    //声明ViewPager
    private ViewPager mViewPager;
    //适配器
    private FragmentPagerAdapter mAdapter;
    //装载Fragment的集合
    private List<Fragment> mFragments;
    //三个Tab对应的布局
    private LinearLayout mTab1,mTab2;
    private TextView mText1,mText2;
    private ImageView iv_return;

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case  R.id.id_candidate:
                    selectTab(0);
                    break;
                case R.id.id_enterprise:
                    selectTab(1);
                    break;
                case R.id.iv_return1:
                    finish();
                    break;
            }
        }
    };

    public void initUI() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

        mTab1 = (LinearLayout) findViewById(R.id.id_candidate);
        mTab2 = (LinearLayout) findViewById(R.id.id_enterprise);

        mText1=(TextView) findViewById(R.id.tv1);
        mText2=(TextView) findViewById(R.id.tv2);

        iv_return=(ImageView) findViewById(R.id.iv_return1);
    }

    private void initEvents(){
        //设置两个Tab的点击事件
        mTab1.setOnClickListener(listener);
        mTab2.setOnClickListener(listener);
        iv_return.setOnClickListener(listener);
    }

    private void initDatas(){
        mFragments=new ArrayList<>();
        //将两个Fragment加入集合中
        mFragments.add(new CandidateFragment());
        mFragments.add(new EnterpriseFragment());

        //初始化适配器
        mAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };

        //设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //将ViewPager的预加载个数设置为你的所有Fragment的数量,解决滑动时候轮播图出现空白的问题
        mViewPager.setOffscreenPageLimit(mFragments.size());
        //设置ViewPager的切换监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面滚动事件
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //页面选中事件
            @Override
            public void onPageSelected(int position) {
                //设置position对应的集合中的fragment
                mViewPager.setCurrentItem(position);
                resetImgs();
                selectTab(position);
            }
            //页面滚动状态改变事件
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void resetImgs() {
        mText1.setTextColor(getResources().getColor(R.color.black));
        mText2.setTextColor(getResources().getColor(R.color.black));
        mText1.setTextSize(20);
        mText2.setTextSize(20);
    }

    //根据点击的Tab设置对应的ImageButton为选中的颜色
    private void selectTab(int i) {
        switch (i) {
            case 0:
                mText1.setTextColor(getResources().getColor(R.color.purple_200));
                mText1.setTextSize(30);
                break;
            case 1:
                mText2.setTextColor(getResources().getColor(R.color.purple_200));
                mText2.setTextSize(30);
                break;
        }
        //设置当前点击的Tab所对应的页面
        mViewPager.setCurrentItem(i);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initUI();
        selectTab(0);//初始页面为主页
        initEvents();//初始化事件
        initDatas();//初始化数据
    }

}